<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="收费类型" prop="priceTypes">
        <el-input
          v-model="queryParams.priceTypes"
          placeholder="请输入收费类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标准价格" prop="priceNormalprice">
        <el-input
          v-model="queryParams.priceNormalprice"
          placeholder="请输入标准价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始日期">
        <el-date-picker
          v-model="daterangePriceStartdate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="辅助价格1" prop="priceOtherpricea">
        <el-input
          v-model="queryParams.priceOtherpricea"
          placeholder="请输入辅助价格1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="辅助价格2" prop="priceOtherpriceb">
        <el-input
          v-model="queryParams.priceOtherpriceb"
          placeholder="请输入辅助价格2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="调价原因" prop="priceReason">
        <el-input
          v-model="queryParams.priceReason"
          placeholder="请输入调价原因"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建者" prop="priceCreateBy">
        <el-input
          v-model="queryParams.priceCreateBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangePriceCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="更新人" prop="priceUpdateBy">
        <el-input
          v-model="queryParams.priceUpdateBy"
          placeholder="请输入更新人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新日期">
        <el-date-picker
          v-model="daterangePriceUpdateDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间">
        <el-date-picker
          v-model="daterangePriceUpdateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="是否启用" prop="pirceUseFlag">
        <el-select v-model="queryParams.pirceUseFlag" placeholder="请选择是否启用" clearable>
          <el-option
            v-for="dict in dict.type.service_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="删除标志" prop="priceDelFlag">
        <el-select v-model="queryParams.priceDelFlag" placeholder="请选择删除标志" clearable>
          <el-option
            v-for="dict in dict.type.service_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['service:price:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['service:price:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['service:price:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['service:price:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="priceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="价格调整表ID" align="center" prop="priceId" />
      <el-table-column label="收费类型" align="center" prop="priceTypes" />
      <el-table-column label="标准价格" align="center" prop="priceNormalprice" />
      <el-table-column label="开始日期" align="center" prop="priceStartdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.priceStartdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="辅助价格1" align="center" prop="priceOtherpricea" />
      <el-table-column label="辅助价格2" align="center" prop="priceOtherpriceb" />
      <el-table-column label="调价原因" align="center" prop="priceReason" />
      <el-table-column label="审批状态" align="center" prop="priceStatus" />
      <el-table-column label="创建者" align="center" prop="priceCreateBy" />
      <el-table-column label="创建时间" align="center" prop="priceCreateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.priceCreateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="priceUpdateBy" />
      <el-table-column label="更新日期" align="center" prop="priceUpdateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.priceUpdateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="priceUpdateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.priceUpdateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center" prop="pirceUseFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.service_yes_no" :value="scope.row.pirceUseFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="删除标志" align="center" prop="priceDelFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.service_yes_no" :value="scope.row.priceDelFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['service:price:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['service:price:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改耗材价格调整对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="收费类型" prop="priceTypes">
          <el-input v-model="form.priceTypes" placeholder="请输入收费类型" />
        </el-form-item>
        <el-form-item label="标准价格" prop="priceNormalprice">
          <el-input v-model="form.priceNormalprice" placeholder="请输入标准价格" />
        </el-form-item>
        <el-form-item label="开始日期" prop="priceStartdate">
          <el-date-picker clearable
            v-model="form.priceStartdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="辅助价格1" prop="priceOtherpricea">
          <el-input v-model="form.priceOtherpricea" placeholder="请输入辅助价格1" />
        </el-form-item>
        <el-form-item label="辅助价格2" prop="priceOtherpriceb">
          <el-input v-model="form.priceOtherpriceb" placeholder="请输入辅助价格2" />
        </el-form-item>
        <el-form-item label="调价原因" prop="priceReason">
          <el-input v-model="form.priceReason" placeholder="请输入调价原因" />
        </el-form-item>
        <el-form-item label="创建者" prop="priceCreateBy">
          <el-input v-model="form.priceCreateBy" placeholder="请输入创建者" />
        </el-form-item>
        <el-form-item label="创建时间" prop="priceCreateTime">
          <el-date-picker clearable
            v-model="form.priceCreateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新人" prop="priceUpdateBy">
          <el-input v-model="form.priceUpdateBy" placeholder="请输入更新人" />
        </el-form-item>
        <el-form-item label="更新日期" prop="priceUpdateDate">
          <el-date-picker clearable
            v-model="form.priceUpdateDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="priceUpdateTime">
          <el-date-picker clearable
            v-model="form.priceUpdateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否启用" prop="pirceUseFlag">
          <el-select v-model="form.pirceUseFlag" placeholder="请选择是否启用">
            <el-option
              v-for="dict in dict.type.service_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="删除标志" prop="priceDelFlag">
          <el-select v-model="form.priceDelFlag" placeholder="请选择删除标志">
            <el-option
              v-for="dict in dict.type.service_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPrice, getPrice, delPrice, addPrice, updatePrice } from "@/api/service/price";

export default {
  name: "Price",
  dicts: ['service_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 耗材价格调整表格数据
      priceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 删除标志时间范围
      daterangePriceStartdate: [],
      // 删除标志时间范围
      daterangePriceCreateTime: [],
      // 删除标志时间范围
      daterangePriceUpdateDate: [],
      // 删除标志时间范围
      daterangePriceUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        priceTypes: null,
        priceNormalprice: null,
        priceStartdate: null,
        priceOtherpricea: null,
        priceOtherpriceb: null,
        priceReason: null,
        priceStatus: null,
        priceCreateBy: null,
        priceCreateTime: null,
        priceUpdateBy: null,
        priceUpdateDate: null,
        priceUpdateTime: null,
        pirceUseFlag: null,
        priceDelFlag: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询耗材价格调整列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangePriceStartdate && '' != this.daterangePriceStartdate) {
        this.queryParams.params["beginPriceStartdate"] = this.daterangePriceStartdate[0];
        this.queryParams.params["endPriceStartdate"] = this.daterangePriceStartdate[1];
      }
      if (null != this.daterangePriceCreateTime && '' != this.daterangePriceCreateTime) {
        this.queryParams.params["beginPriceCreateTime"] = this.daterangePriceCreateTime[0];
        this.queryParams.params["endPriceCreateTime"] = this.daterangePriceCreateTime[1];
      }
      if (null != this.daterangePriceUpdateDate && '' != this.daterangePriceUpdateDate) {
        this.queryParams.params["beginPriceUpdateDate"] = this.daterangePriceUpdateDate[0];
        this.queryParams.params["endPriceUpdateDate"] = this.daterangePriceUpdateDate[1];
      }
      if (null != this.daterangePriceUpdateTime && '' != this.daterangePriceUpdateTime) {
        this.queryParams.params["beginPriceUpdateTime"] = this.daterangePriceUpdateTime[0];
        this.queryParams.params["endPriceUpdateTime"] = this.daterangePriceUpdateTime[1];
      }
      listPrice(this.queryParams).then(response => {
        this.priceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        priceId: null,
        priceTypes: null,
        priceNormalprice: null,
        priceStartdate: null,
        priceOtherpricea: null,
        priceOtherpriceb: null,
        priceReason: null,
        priceStatus: null,
        priceCreateBy: null,
        priceCreateTime: null,
        priceUpdateBy: null,
        priceUpdateDate: null,
        priceUpdateTime: null,
        pirceUseFlag: null,
        priceDelFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangePriceStartdate = [];
      this.daterangePriceCreateTime = [];
      this.daterangePriceUpdateDate = [];
      this.daterangePriceUpdateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.priceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加耗材价格调整";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const priceId = row.priceId || this.ids
      getPrice(priceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改耗材价格调整";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.priceId != null) {
            updatePrice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPrice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const priceIds = row.priceId || this.ids;
      this.$modal.confirm('是否确认删除耗材价格调整编号为"' + priceIds + '"的数据项？').then(function() {
        return delPrice(priceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('service/price/export', {
        ...this.queryParams
      }, `price_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
